package com.yy.main.app.web;

import com.github.pagehelper.PageInfo;
import com.yy.kit.annotation.HasPers;
import com.yy.kit.collections.YYMapKits;
import com.yy.kit.string.YYStrKits;
import com.yy.main.app.mapper.TKeywordMessageMapper;
import com.yy.main.app.mapper.TMenuMessageMapper;
import com.yy.main.app.mapper.TMessageMapper;
import com.yy.main.app.service.IKeywordMessageService;
import com.yy.main.app.service.IMenuMessageService;
import com.yy.main.app.service.IMessageService;
import com.yy.main.app.web.base.CommonController;
import com.yy.pojo.base.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 支付宝或微信消息表 前端控制器
 * </p>
 *
 * @author 杨阳
 * @since 2017-09-20
 */
@Controller
@RequestMapping("/message")
@Api(value = "消息管理controller", tags = {"消息管理controller"})
public class MessageController extends CommonController {

    @Resource(name = "iMessageService")
    private IMessageService iMessageService;
    @Resource(name = "iMenuMessageService")
    private IMenuMessageService iMenuMessageService;
    @Resource(name = "iKeywordMessageService")
    private IKeywordMessageService iKeywordMessageService;
    @Autowired
    private TMessageMapper tMessageMapper;
    @Autowired
    private TKeywordMessageMapper tKeywordMessageMapper;
    @Autowired
    private TMenuMessageMapper tMenuMessageMapper;


    /**
     * <p>
     * 跳转到消息管理页面
     * </p>
     *
     * @param session 应用会话
     * @return {@link ModelAndView}
     * @author 杨阳
     * @since 2017年9月20日
     */
    @ApiOperation(value = "跳转到消息管理页面", httpMethod = "GET", notes = "跳转到消息管理页面,需要拥有message:manager权限")
    @RequestMapping(value = "/messageList", method = RequestMethod.GET)
    @HasPers(name = "message:manager")
    public ModelAndView messageList(HttpSession session) {
        return goToPageByModelAndView("system/message/message_list", null, getCurrentUser(session).getId());
    }

    /**
     * <p>
     * 后台管理，获得消息管理的数据表格
     * </p>
     *
     * @param page    当前页
     * @param limit   每页显示记录数
     * @param type    消息类型,1. 文本消息 2.图文消息
     * @param content 搜索的是文本消息的内容
     * @param title   搜索的是图文消息的标题
     * @return {@link Map}<String, Object>
     */
    @ApiOperation(value = "后台管理，获得消息管理的数据表格", httpMethod = "GET", notes = "后台管理，获得消息管理的数据表格")
    @RequestMapping(value = "/messageTable", method = RequestMethod.GET)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "当前页数", required = true),
            @ApiImplicitParam(name = "limit", value = "每页记录数", required = true),
            @ApiImplicitParam(name = "type", value = "消息类型,1. 文本消息 2.图文消息"),
            @ApiImplicitParam(name = "content", value = "搜索的是文本消息的内容"),
            @ApiImplicitParam(name = "title", value = "搜索的是图文消息的标题")
    })
    public @ResponseBody
    Map<String, Object> messageTable(Integer page, Integer limit, String type, String content, String title) {
        TMessageExample example = new TMessageExample();
        TMessageExample.Criteria criteria = example.createCriteria();

        if (YYStrKits.isNotBlank(type)) {
            criteria.andTypeEqualTo(type);
        }
        if (YYStrKits.isNotBlank(content)) {
            criteria.andContentLike(content);
        }
        if (YYStrKits.isNotBlank(title)) {
            criteria.andTitleLike(title);
        }
        List<TMessage> msgList = tMessageMapper.selectByExample(example);
        return super.pageToMap(new PageInfo(msgList));
    }

    /**
     * <p>
     * 跳转到编辑消息的页面
     * </p>
     *
     * @param id 消息ID
     * @return {@link ModelAndView}
     * @author 杨阳
     * @since 2017年9月20日
     */
    @ApiOperation(value = "跳转到编辑消息的页面", httpMethod = "GET", notes = "跳转到编辑消息的页面，需要拥有message:add,message:update权限")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "消息ID", paramType = "path")
    })
    @RequestMapping(value = "/messageform/{id}", method = RequestMethod.GET)
    @HasPers(name = "message:add,message:update")
    public ModelAndView messageForm(@PathVariable("id") String id, HttpServletRequest request) {
        TUser user = getCurrentUser(request.getSession());
        TMessage msg = tMessageMapper.selectByPrimaryKey(id);// 如果是新增id肯定是-1
        if (null == msg) {
            msg = new TMessage();
            msg.setId("-1");
        }
        Map<String, Object> param = YYMapKits.createHashMap();
        param.put("message", msg);
        return goToPageByModelAndView("system/message/message_form", param, user.getId());
    }

    /**
     * <p>
     * 保存消息信息
     * </p>
     *
     * @param msg 消息实体
     * @return {@link Map}
     * @author 杨阳
     * @since 2017年9月20日
     */
    @ApiOperation(value = "保存消息信息", httpMethod = "POST", notes = "增加或修改消息")
    @RequestMapping(value = "/savemessage", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> savemessage(TMessage msg) {
        Map<String, Object> result = YYMapKits.createHashMap();
        if (YYStrKits.isBlank(msg.getId()) || "-1".equals(msg.getId())) {// 增加
            msg.setId(YYStrKits.getTableId());
            msg.setCreate_date(new Date());
            Integer b = tMessageMapper.insert(msg);
            if (b > 0) {
                result.put("success", Boolean.TRUE);
                result.put("msg", "增加消息成功!!");
            } else {
                result.put("success", Boolean.FALSE);
                result.put("msg", "增加消息失败，请重试!!");
            }
        } else {// 修改
            msg.setUpdate_date(new Date());
            Integer b = tMessageMapper.updateByPrimaryKeySelective(msg);
            if (b > 0) {
                result.put("success", Boolean.TRUE);
                result.put("msg", "修改消息成功!");
            } else {
                result.put("success", Boolean.FALSE);
                result.put("msg", "修改消息失败，请重试!");
            }
        }
        return result;
    }

    /**
     * <p>
     * 删除消息
     * </p>
     *
     * @param id 消息id
     * @return {@link Map}
     * @author 杨阳
     * @since 2017年9月20日
     */
    @ApiOperation(value = "删除消息", httpMethod = "POST", notes = "删除消息,需要拥有message:delete权限")
    @RequestMapping(value = "/delmessage/{id}", method = RequestMethod.POST)
    @HasPers(name = "message:delete")
    public @ResponseBody
    Map<String, Object> delmessage(@PathVariable("id") String id) {
        Map<String, Object> map = YYMapKits.createHashMap();

        // 查看消息是否与关键字相关联
        TKeywordMessageExample example = new TKeywordMessageExample();
        example.createCriteria().andMessage_idEqualTo(id);
        long i = tKeywordMessageMapper.countByExample(example);

        if (i > 0) {
            map.put("success", Boolean.FALSE);
            map.put("msg", "要删除的消息与关键字相关联,不可以删除!");
            return map;
        }

        // 查看消息是否与菜单相关联
        TMenuMessageExample example1 = new TMenuMessageExample();
        example1.createCriteria().andMessage_idEqualTo(id);
        i = tMenuMessageMapper.countByExample(example1);

        if (i > 0) {
            map.put("success", Boolean.FALSE);
            map.put("msg", "要删除的消息与菜单相关联,不可以删除!");
            return map;
        }

        Integer b = tMessageMapper.deleteByPrimaryKey(id);
        if (b > 0) {
            map.put("success", Boolean.TRUE);
            map.put("msg", "删除成功!");
        } else {
            map.put("success", Boolean.FALSE);
            map.put("msg", "删除失败，请重试!");
        }
        return map;
    }

    /**
     * <p>
     * 查看消息详情
     * </p>
     *
     * @return {@link ModelAndView}
     * @author 杨阳
     * @since 2017年9月20日
     */
    @RequestMapping(value = "/show/{id}", method = RequestMethod.GET)
    @HasPers(name = "message:show")
    @ApiOperation(value = "查看消息", httpMethod = "GET", notes = "查消息,需要拥有message:show权限")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "消息ID", required = true, paramType = "path")
    })
    public ModelAndView show(@PathVariable("id") String id) {
        TMessage msg = tMessageMapper.selectByPrimaryKey(id);
        Map<String, Object> param = YYMapKits.createHashMap();
        param.put("message", msg);
        return goToPageByModelAndView("system/message/message_show", param);
    }

}
